package THREADS;

import org.junit.runner.Runner;

import DeportManage.*;
import LOG.Logger;
import MVC.*;


public class WorkThread implements Runnable{
	private DataObserver data_observer;
	private int no_worker;
	private int seq_No;
	
	/**
	 * Constructor to create an object with the no. of worker and DataObserver
	 * @param no the number of worker (1,2,3)
	 * @param data_observer the object of DataObserver
	 */
	public WorkThread(int no,DataObserver data_observer){
		this.no_worker = no;
		this.data_observer = data_observer;
	}
	/**
	 * Method to perform the thread method
	 */
	@Override
	public void run() {
		while(data_observer.get_Run())
		{
			if(!data_observer.getCustList().getCustomerList().isEmpty()){
				try {Thread.sleep(data_observer.getSpeed());} 
				catch (InterruptedException e) {e.printStackTrace();}	
				
				try{
					this.seq_No = data_observer.getSequnce_no();
					Customer cm = data_observer.getCustList().searchCustomerBySequenceNo(seq_No);
					if(cm != null){
						data_observer.processCollection(cm, String.valueOf(no_worker));	
						data_observer.removeCustomer(cm);
						data_observer.setSequence_no(seq_No+1);
					}
				}catch (Exception e) {}
			}
			else
				break;
		}

		data_observer.setState(true);
		switch(no_worker){
		case 1:
			Logger.writeLog("Worker Yanchao's process of collection is successfully finished!");
			break;
		case 2:
			Logger.writeLog("Worker Madiha's process of collection is successfully finished!");
			break;
		case 3:
			Logger.writeLog("Worker Mengting's process of collection is successfully finished!");
			break;	
		}
	}
}
